Guest User

Untitled

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