# Path: app/py/cuda_addonman/work_remote.py
# def get_url(url, fn, del_first=False):
#     fn_temp = fn+'.download'
#     if os.path.isfile(fn_temp):
#         os.remove(fn_temp)
#     if del_first and os.path.isfile(fn):
#         os.remove(fn)
# 
#     if opt.proxy:
#         proxies = { 'http': opt.proxy, 'https': opt.proxy, }
#     else:
#         proxies = None
#     #print('proxy', proxies)
# 
#     while True:
#         try:
#             r = requests.get(url, proxies=proxies, stream=True)
#             with open(fn_temp, 'wb') as f:
#                 for chunk in r.iter_content(chunk_size=1024):
#                     if chunk: # filter out keep-alive new chunks
#                         f.write(chunk)
#                         #f.flush() commented by recommendation
# 
#             if os.path.isfile(fn_temp):
#                 if os.path.isfile(fn):
#                     os.remove(fn)
#                 os.rename(fn_temp, fn)
#             return
# 
#         except Exception as e:
#             res = app.msg_box(_('Cannot download:\n{}\n{}\n\nRetry?').format(url, str(e)),
#                 app.MB_ABORTRETRYIGNORE + app.MB_ICONWARNING)
#             if res==app.ID_IGNORE: return
#             if res==app.ID_ABORT: return False
#             if res==app.ID_CANCEL: return False


import os
import json
import tempfile
import subprocess
import cudatext_cmd
from datetime import datetime
from cudatext import *
from .work_remote import get_url
from .work_install_helper import after_install
from cudax_lib import get_translation

_   = get_translation(__file__)  # i18n

def get_datetime_short():
    t = datetime.now()
    return t.strftime('%Y.%m.%d')

def get_branch(url):

    fn = os.path.join(tempfile.gettempdir(), 'cudatext_git_branches.json')
    url_branches = url.replace('http://', 'https://').replace('//github.com/', '//api.github.com/repos/') + '/branches'
    get_url(url_branches, fn, True)
    if not os.path.isfile(fn):
        msg_box(_('Cannot read GitHub list of branches for that repo'), MB_OK+MB_ICONERROR)
        return

    with open(fn, 'r') as f:
        data = json.load(f)

# Path: app/py/cuda_addonman/work_remote.py
# def get_url(url, fn, del_first=False):
#     fn_temp = fn+'.download'
#     if os.path.isfile(fn_temp):
#         os.remove(fn_temp)
#     if del_first and os.path.isfile(fn):
#         os.remove(fn)
# 
#     if opt.proxy:
#         proxies = { 'http': opt.proxy, 'https': opt.proxy, }
#     else:
#         proxies = None
#     #print('proxy', proxies)
# 
#     while True:
#         try:
#             r = requests.get(url, proxies=proxies, stream=True)
#             with open(fn_temp, 'wb') as f:
#                 for chunk in r.iter_content(chunk_size=1024):
#                     if chunk: # filter out keep-alive new chunks
#                         f.write(chunk)
#                         #f.flush() commented by recommendation
# 
#             if os.path.isfile(fn_temp):
#                 if os.path.isfile(fn):
#                     os.remove(fn)
#                 os.rename(fn_temp, fn)
#             return
# 
#         except Exception as e:
#             res = app.msg_box(_('Cannot download:\n{}\n{}\n\nRetry?').format(url, str(e)),
#                 app.MB_ABORTRETRYIGNORE + app.MB_ICONWARNING)
#             if res==app.ID_IGNORE: return
#             if res==app.ID_ABORT: return False
#             if res==app.ID_CANCEL: return False


# Path: app/py/cuda_addonman/work_github.py
import os
import json
import tempfile
import subprocess
import cudatext_cmd
from datetime import datetime
from cudatext import *
from .work_remote import get_url
from .work_install_helper import after_install
from cudax_lib import get_translation


_   = get_translation(__file__)  # i18n

def get_datetime_short():
    t = datetime.now()
    return t.strftime('%Y.%m.%d')

def get_branch(url):

    fn = os.path.join(tempfile.gettempdir(), 'cudatext_git_branches.json')
    url_branches = url.replace('http://', 'https://').replace('//github.com/', '//api.github.com/repos/') + '/branches'
    get_url(url_branches, fn, True)
    if not os.path.isfile(fn):
        msg_box(_('Cannot read GitHub list of branches for that repo'), MB_OK+MB_ICONERROR)
        return

    with open(fn, 'r') as f:
        data = json.load(f)

        if not isinstance(data, list):