Guest User

Untitled

a guest
Dec 27th, 2021
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.06 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. # RECOMMENDATIONS
  4. #
  5. # 1. Backup your state file
  6.  
  7.  
  8. import json
  9. import urllib.request
  10.  
  11. import sys
  12. import logging
  13. from optparse import OptionParser
  14. import os.path
  15. import shutil
  16.  
  17.  
  18. logging.basicConfig(level=logging.DEBUG)
  19.  
  20. def set_working_directory():
  21.     import os
  22.     new_wd = os.path.dirname(sys.argv[0])
  23.     os.chdir(new_wd)
  24.     logging.debug(new_wd)
  25.  
  26. set_working_directory()
  27.  
  28. STATE_FILE = "download_latest.state"
  29. # PROJECT_URL = "signalapp/Signal-Desktop"
  30.  
  31. usage = "usage: download_latest.py [options] PROJECT_URL ARTIFACT_NAME ARCHIVE_FORMAT"
  32. parser = OptionParser(usage)
  33. parser.add_option("-n", "--name", default="", dest="subfolder", help="name of local subfolder")
  34. parser.add_option("--log", default="DEBUG", dest="loglevel", help="loglevel")
  35.  
  36. (options, args) = parser.parse_args()
  37.  
  38. if len(args) != 3:
  39.     print("Incorrect number of arguments!")
  40.     print(usage)
  41.     exit()
  42.  
  43. PROJECT_URL = args[0]
  44. logging.debug(PROJECT_URL)
  45. ARTIFACT_NAME = args[1]
  46. logging.debug(ARTIFACT_NAME)
  47. ARCHIVE_FORMAT = args[2]
  48. logging.debug(ARCHIVE_FORMAT)
  49.  
  50. # logging.basicConfig(level=options.loglevel)
  51.  
  52. SUBFOLDER = PROJECT_URL.split("/")
  53. logging.debug(f"SUBFOLDER={SUBFOLDER}")
  54.  
  55. if len(SUBFOLDER) != 2:
  56.     print("Invalid project url {}: wrong number of slashes (1 expected)!")
  57.     exit()
  58.  
  59. SUBFOLDER = SUBFOLDER[1]
  60.  
  61. if options.subfolder != "":
  62.     SUBFOLDER = options.subfolder
  63.  
  64. logging.debug(SUBFOLDER)
  65.  
  66. # load state file
  67. if os.path.isfile(STATE_FILE):
  68.     with open(STATE_FILE) as f:
  69.         STATE = json.loads("\n".join(f.readlines()))
  70. else:
  71.     STATE = {}
  72.  
  73.  
  74. # read json api object from github
  75.  
  76. #logging.debug(urllib.request.urlopen("https://api.github.com/repos/{}/releases/latest".format(PROJECT_URL)).read())
  77.  
  78. json_response = json.loads(urllib.request.urlopen("https://api.github.com/repos/{}/releases/latest".format(PROJECT_URL)).read())
  79.  
  80. logging.debug(json_response['name'])
  81.  
  82. remote_version = json_response['name']
  83. if SUBFOLDER in STATE:
  84.     local_version = STATE[SUBFOLDER]
  85.     if local_version == remote_version:
  86.         print("remote version ({}) equals local version ({}). Nothing to do, quitting!".format(remote_version, local_version))
  87.         exit()
  88.  
  89. if ARTIFACT_NAME not in json_response:
  90.     print("Artifact name not in json response. Check please!")
  91.     exit()
  92.  
  93. artifact_url = json_response[ARTIFACT_NAME]
  94. local_pkg, headers  = urllib.request.urlretrieve(artifact_url)
  95. logging.debug(f"local_pkg = {local_pkg}")
  96.  
  97. # remove old version
  98. if os.path.isdir(SUBFOLDER):
  99.     shutil.rmtree(SUBFOLDER)
  100.  
  101.  
  102. # does not detect nested folders FIXME
  103. match ARCHIVE_FORMAT:
  104.     case "zip":
  105.         import zipfile
  106.         with zipfile.open(local_pkg) as z:
  107.             z.extract_all(SUBFOLDER)
  108.  
  109.     case "tgz":
  110.         import subprocess
  111.         import os
  112.         os.mkdir(SUBFOLDER)
  113.         os.chdir(SUBFOLDER)
  114.         rv = subprocess.call(["tar", "xf", local_pkg])
  115.         os.chdir("..")
  116.  
  117.     case _:
  118.         print(f"archive format {ARCHIVE_FORMAT} not known. Exiting!")
  119.         exit()
  120.  
  121. STATE[SUBFOLDER] = remote_version
  122.  
  123. logging.debug(f"remote={remote_version};state={STATE[SUBFOLDER]}")
  124.  
  125.  
  126. with open(STATE_FILE, 'w') as f:
  127.     f.write(json.dumps(STATE))
  128.  
Advertisement
Add Comment
Please, Sign In to add comment