|
1 | | -from sys import version_info, stdout |
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import sys |
2 | 4 |
|
3 | 5 | # Before we do ANYTHING, we check to make sure python is the correct version! |
4 | 6 |
|
5 | | -if version_info < (3,6,0): |
| 7 | +if sys.version_info < (3,6,0): |
6 | 8 |
|
7 | | - stdout.write("\n--== [ Invalid python version! ] ==--\n") |
8 | | - stdout.write("Current version: {}\n".format(version_info)) |
9 | | - stdout.write("Expected version: 3.6+\n") |
10 | | - stdout.write("\nPlease install the correct version of python before continuing!\n") |
| 9 | + sys.stdout.write("\n--== [ Invalid python version! ] ==--\n") |
| 10 | + sys.stdout.write("Current version: {}\n".format(version_info)) |
| 11 | + sys.stdout.write("Expected version: 3.6+\n") |
| 12 | + sys.stdout.write("\nPlease install the correct version of python before continuing!\n") |
11 | 13 |
|
12 | | - exit() |
| 14 | + sys.exit() |
13 | 15 |
|
14 | 16 | import tempfile |
15 | 17 | import urllib.request |
|
31 | 33 | A Set of tools to automate the server update process. |
32 | 34 | """ |
33 | 35 |
|
34 | | -__version__ = '2.1.0' |
| 36 | +__version__ = '2.2.0' |
| 37 | + |
| 38 | +# These variables contain links for the script updating process. |
| 39 | + |
| 40 | +GITHUB = 'https://github.com/Owen-Cochell/PaperMC-Update' |
| 41 | +GITHUB_RELEASE = 'https://api.github.com/repos/Owen-Cochell/PaperMC-Update/releases/latest' |
| 42 | +GITHUB_RAW = 'https://raw.githubusercontent.com/Owen-Cochell/PaperMC-Update/master/server_update.py' |
35 | 43 |
|
36 | 44 |
|
37 | 45 | def load_config(config: str) -> Tuple[str, int]: |
@@ -74,6 +82,78 @@ def load_config(config: str) -> Tuple[str, int]: |
74 | 82 | return version, build |
75 | 83 |
|
76 | 84 |
|
| 85 | +def upgrade_script(serv: ServerUpdater): |
| 86 | + """ |
| 87 | + Upgrades this script. |
| 88 | + |
| 89 | + We do this by checking github for any new releases, |
| 90 | + comparing them to our version, |
| 91 | + and then updating if necessary. |
| 92 | + |
| 93 | + We use the ServerUpdater to do this operation for us, |
| 94 | + so you will need to provide it for this function |
| 95 | + to work correctly. |
| 96 | +
|
| 97 | + :param serv: ServerUpdater to use |
| 98 | + :type serv: ServerUpdater |
| 99 | + """ |
| 100 | + |
| 101 | + output("# Checking for update ...") |
| 102 | + |
| 103 | + # Creating request here: |
| 104 | + |
| 105 | + req = urllib.request.Request(GITHUB_RELEASE, headers={'Accept': 'application/vnd.github.v3+json'}) |
| 106 | + |
| 107 | + # Getting data: |
| 108 | + |
| 109 | + data = json.loads(urllib.request.urlopen(req).read()) |
| 110 | + |
| 111 | + # Checking if the version is new: |
| 112 | + |
| 113 | + if data['tag_name'] == __version__: |
| 114 | + |
| 115 | + # No update necessary, lets log and exit: |
| 116 | + |
| 117 | + output("# No update necessary!\n") |
| 118 | + |
| 119 | + return |
| 120 | + |
| 121 | + output("# New version available!") |
| 122 | + |
| 123 | + url = GITHUB_RAW |
| 124 | + path = os.path.realpath(__file__) |
| 125 | + |
| 126 | + # Determine if we are working in a frozen environment: |
| 127 | + |
| 128 | + if getattr(sys, 'frozen', False): |
| 129 | + |
| 130 | + print("# Can't upgrade frozen files!") |
| 131 | + |
| 132 | + return |
| 133 | + |
| 134 | + # Getting data: |
| 135 | + |
| 136 | + data = urllib.request.urlopen(urllib.request.Request(url)) |
| 137 | + |
| 138 | + # Write the data: |
| 139 | + |
| 140 | + serv.fileutil.create_temp_dir() |
| 141 | + |
| 142 | + temp_path = os.path.realpath(serv.fileutil.temp.name + '/temp') |
| 143 | + |
| 144 | + file = open(temp_path, mode='wb') |
| 145 | + |
| 146 | + file.write(data.read()) |
| 147 | + |
| 148 | + # Install the new script: |
| 149 | + |
| 150 | + serv.fileutil.install(temp_path, path) |
| 151 | + |
| 152 | + # We are done! |
| 153 | + |
| 154 | + output("# Script update complete!\n") |
| 155 | + |
| 156 | + |
77 | 157 | def output(text: str): |
78 | 158 |
|
79 | 159 | """ |
@@ -173,14 +253,14 @@ def progress_bar(length: int, stepsize: int, total_steps: int, step: int, prefix |
173 | 253 |
|
174 | 254 | if not args.quiet: |
175 | 255 |
|
176 | | - stdout.write("{}[{}{}] {}/{}\r".format(prefix, prog_char*x, empty_char*(size-x), |
| 256 | + sys.stdout.write("{}[{}{}] {}/{}\r".format(prefix, prog_char*x, empty_char*(size-x), |
177 | 257 | (step*stepsize if step < total_steps - 1 else length), length)) |
178 | | - stdout.flush() |
| 258 | + sys.stdout.flush() |
179 | 259 |
|
180 | 260 | if not args.quiet and step >= total_steps - 1 : |
181 | 261 |
|
182 | | - stdout.write("\n") |
183 | | - stdout.flush() |
| 262 | + sys.stdout.write("\n") |
| 263 | + sys.stdout.flush() |
184 | 264 |
|
185 | 265 |
|
186 | 266 | class Update: |
@@ -342,7 +422,7 @@ def download_response(self, version: str, build_num:int) -> HTTPResponse: |
342 | 422 |
|
343 | 423 | def download_file(self, path: str, version: str, build_num:int, check:bool=True, call: Callable=None, args: List=None, blocksize: int=4608) -> str: |
344 | 424 | """ |
345 | | - Donloads the content to the given external file. |
| 425 | + Downloads the content to the given external file. |
346 | 426 | We handle all file operations, |
347 | 427 | and automatically work with the URLResponse objects |
348 | 428 | to write the file contents to an external file. |
@@ -910,22 +990,19 @@ def __init__(self, path, config_file: str='', version='0', build=-1, config=True |
910 | 990 | self.integrity = integrity # Boolean determining if we should run an integrity check |
911 | 991 | self.version_install = None # Version to install |
912 | 992 | self.build_install = None # Build to install |
913 | | - |
914 | | - # Starting object |
915 | | - |
916 | | - self._start(config) |
| 993 | + self.config = config |
917 | 994 |
|
918 | 995 | self.update = Update() # Updater Instance |
919 | 996 |
|
920 | | - def _start(self, config): |
| 997 | + def start(self): |
921 | 998 | """ |
922 | 999 | Starts the object, loads configuration. |
923 | 1000 | """ |
924 | 1001 |
|
925 | 1002 | temp_version = '0' |
926 | 1003 | temp_build = 0 |
927 | 1004 |
|
928 | | - if config: |
| 1005 | + if self.config: |
929 | 1006 |
|
930 | 1007 | # Allowed to use configuration file |
931 | 1008 |
|
@@ -1483,9 +1560,15 @@ def _select(self, val: Any, choice: Sequence[Any], default: str, name: str, prin |
1483 | 1560 |
|
1484 | 1561 | if val == 'current': |
1485 | 1562 |
|
1486 | | - # User wants currently installed version: |
| 1563 | + if name == 'version': |
1487 | 1564 |
|
1488 | | - val = self.version |
| 1565 | + # User wants currently installed version: |
| 1566 | + |
| 1567 | + val = self.version |
| 1568 | + |
| 1569 | + elif name == 'build': |
| 1570 | + |
| 1571 | + val = self.buildnum |
1489 | 1572 |
|
1490 | 1573 | if val not in choice: |
1491 | 1574 |
|
@@ -1529,15 +1612,15 @@ def _url_report(self, point: str): |
1529 | 1612 | epilog="Please check the github page for more info: " |
1530 | 1613 | "https://github.com/Owen-Cochell/PaperMC-Update.") |
1531 | 1614 |
|
1532 | | - parser.add_argument('path', help='Path to paper jar file') |
| 1615 | + parser.add_argument('path', help='Path to paper jar file', default=os.path.dirname(__file__) + '/', nargs='?') |
1533 | 1616 |
|
1534 | 1617 | version = parser.add_argument_group('Version Options', 'Arguments for selecting and altering server version information') |
1535 | 1618 |
|
1536 | 1619 | # +===========================================+ |
1537 | 1620 | # Server version arguments: |
1538 | 1621 |
|
1539 | 1622 | version.add_argument('-v', '--version', help='Server version to install(Sets default value)', default='latest', type=str) |
1540 | | - version.add_argument('-b', '--build', help='Server build to install(Sets default value)', default=-1, type=int) |
| 1623 | + version.add_argument('-b', '--build', help='Server build to install(Sets default value)', default=-1, type=str) |
1541 | 1624 | version.add_argument('-iv', help='Sets the currently installed server version, ignores config', default='0', type=str) |
1542 | 1625 | version.add_argument('-ib', help='Sets the currently installed server build, ignores config', default=0, type=int) |
1543 | 1626 | version.add_argument('-sv', '--server-version', help="Displays server version from configuration file and exits", action='store_true') |
@@ -1568,6 +1651,7 @@ def _url_report(self, point: str): |
1568 | 1651 | action='store_true') |
1569 | 1652 | parser.add_argument('-s', '--stats', help='Displays statistics on the selected version and build', action='store_true') |
1570 | 1653 | parser.add_argument('-V', '--script-version', help='Displays script version', version=__version__, action='version') |
| 1654 | + parser.add_argument('-u', '--upgrade', help='Upgrades this script to a new version if necessary, and exits', action='store_true') |
1571 | 1655 |
|
1572 | 1656 | # Deprecated arguments - Included for compatibility, but do nothing |
1573 | 1657 |
|
@@ -1595,6 +1679,18 @@ def _url_report(self, point: str): |
1595 | 1679 |
|
1596 | 1680 | update_available = True |
1597 | 1681 |
|
| 1682 | + # Determine if we should upgrade: |
| 1683 | + |
| 1684 | + if args.upgrade: |
| 1685 | + |
| 1686 | + upgrade_script(serv) |
| 1687 | + |
| 1688 | + sys.exit() |
| 1689 | + |
| 1690 | + # Start the server updater |
| 1691 | + |
| 1692 | + serv.start() |
| 1693 | + |
1598 | 1694 | # Figure out the output name: |
1599 | 1695 |
|
1600 | 1696 | name = None |
|
0 commit comments