def download_repo_github(repo, target_directory, version=None): """Download a repo of a particular version from from github. Args: repo: The name of a repo to be downloaded (eg. 'lbt-grasshopper'). target_directory: the directory where the library should be downloaded to. version: The version of the repository to download. If None, the most recent version will be downloaded. (Default: None) """ # download files if version is None: url = "https://github.com/ladybug-tools/{}/archive/master.zip".format( repo) else: url = "https://github.com/ladybug-tools/{}/archive/v{}.zip".format( repo, version) zip_file = os.path.join(target_directory, '%s.zip' % repo) print('Downloading "{}" github repository to: {}'.format( repo, target_directory)) download_file_by_name(url, target_directory, zip_file) #unzip the file unzip_file(zip_file, target_directory) # try to clean up the downloaded zip file try: os.remove(zip_file) except: print('Failed to remove downloaded zip file: {}.'.format(zip_file)) # return the directory where the unzipped files live if version is None: return os.path.join(target_directory, '{}-master'.format(repo)) else: return os.path.join(target_directory, '{}-{}'.format(repo, version))
def test_unzip_file(): """Test the unzip file capability""" folder = "./tests/zip/extracted" wf_path = "./tests/zip/test.zip" futil.unzip_file(wf_path, folder) extracted_epw = os.path.join(folder, "AUS_NSW.Sydney.947670_IWEC.epw") assert os.path.isfile(extracted_epw) assert 1500000 < os.stat(extracted_epw).st_size < 1600000 futil.nukedir(folder)
def model_to_rad_folder(model_file, folder, view, grid, full_match, config_file, minimal, no_grid_check, no_view_check, log_file): """Translate a Model file into a Radiance Folder. \b Args: model_file: Full path to a Model JSON file (HBJSON) or a Model pkl (HBpkl) file. This can also be a zipped version of a Radiance folder, in which case this command will simply unzip the file into the --folder and no other operations will be performed on it. """ try: # set the default folder if it's not specified if folder is None: folder = os.path.dirname(os.path.abspath(model_file)) # first check to see if the model_file is a zipped radiance folder if zipfile.is_zipfile(model_file): unzip_file(model_file, folder) log_file.write(folder) else: # re-serialize the Model and perform any checks model = Model.from_file(model_file) if not no_grid_check and len( model.properties.radiance.sensor_grids) == 0: raise ValueError( 'Model contains no sensor grids. These are required.') if not no_view_check and len(model.properties.radiance.views) == 0: raise ValueError('Model contains no views These are required.') # translate the model to a radiance folder rad_fold = model.to.rad_folder(model, folder, config_file, minimal, views=view, grids=grid, full_match=full_match) log_file.write(rad_fold) except Exception as e: _logger.exception('Model translation failed.\n{}'.format(e)) sys.exit(1) else: sys.exit(0)
def download_repo_github(repo, target_directory, version=None): """Download a repo of a particular version from from github. Args: repo: The name of a repo to be downloaded (eg. 'lbt-grasshopper'). target_directory: The directory where the library should be downloaded. version: The version of the repository to download. If None, the most recent version will be downloaded. (Default: None) """ # download files if version is None: url = 'https://github.com/ladybug-tools/{}/archive/master.zip'.format( repo) else: url = 'https://github.com/ladybug-tools/{}/archive/v{}.zip'.format( repo, version) zip_file = os.path.join(target_directory, '%s.zip' % repo) print('Downloading "{}" github repository to: {}'.format( repo, target_directory)) try: download_file_by_name(url, target_directory, zip_file) except ValueError: msg = 'Access is denied to: {}\nMake sure that you are running this command as' \ ' an Administrator by right-clicking on\nRhino and selecting "Run As ' \ 'Administrator" before opening Grasshopper and\n running this ' \ 'component.'.format(target_directory) print(msg) raise ValueError(msg) # unzip the file unzip_file(zip_file, target_directory) # try to clean up the downloaded zip file try: os.remove(zip_file) except Exception: print('Failed to remove downloaded zip file: {}.'.format(zip_file)) # return the directory where the unzipped files live if version is None: return os.path.join(target_directory, '{}-master'.format(repo)) else: return os.path.join(target_directory, '{}-{}'.format(repo, version))
# onebuilding URL type _folder_name = _weather_URL.split('/')[-1][:-4] else: # dept of energy URL type _folder_name = _weather_URL.split('/')[-2] # create default working_dir if _folder_ is None: _folder_ = os.path.join(os.environ['USERPROFILE'], 'ladybug', _folder_name) try: _folder_.decode('ascii') except UnicodeDecodeError: raise UnicodeDecodeError('\nYour USERNAME contains a non-ASCII character, meaning files are downloaded to: \n {}' '\nUse the _folder_ input to this component to download EPW files to a valid location.'.format(_folder_)) else: print 'Files will be downloaded to: {}'.format(_folder_) # default file names epw = os.path.join(_folder_, _folder_name + '.epw') stat = os.path.join(_folder_, _folder_name + '.stat') ddy = os.path.join(_folder_, _folder_name + '.ddy') # download and unzip the files if they do not exist if not os.path.isfile(epw) or not os.path.isfile(stat) or not os.path.isfile(ddy): zip_file_path = os.path.join(_folder_, _folder_name + '.zip') download_file(_weather_URL, zip_file_path, True) unzip_file(zip_file_path) # set output epw_file, stat_file, ddy_file = epw, stat, ddy