Created
February 17, 2018 22:48
-
-
Save olofk/18b6aed1c26197debb11ee92f2ea8fe7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import os.path | |
import subprocess | |
import sys | |
if sys.version[0] == '2': | |
import ConfigParser as configparser | |
else: | |
import configparser | |
logger = logging.getLogger(__name__) | |
BASE_LIBS = ['https://github.com/openrisc/orpsoc-cores', | |
'https://github.com/fusesoc/fusesoc-cores'] | |
def upgrade_config(f): | |
config = configparser.SafeConfigParser() | |
config.read(f) | |
libraries = [] | |
cores_root = [] | |
systems_root = [] | |
try: | |
systems_root.append(config.get('main', 'systems_root')) | |
except (configparser.NoOptionError, configparser.NoSectionError): | |
pass | |
try: | |
cores_root = config.get('main', 'cores_root').split() | |
except (configparser.NoOptionError, configparser.NoSectionError): | |
pass | |
for c in systems_root + cores_root: | |
print("Converting {} to library".format(c)) | |
absc = os.path.normpath(os.path.expanduser(os.path.expandvars(c))) | |
#Ignore non-existing directories | |
if not os.path.isdir(absc): | |
logger.warn("'{}' does not exist. Ignoring".format(c)) | |
continue | |
name = os.path.basename(absc) | |
location = c | |
#Check if dir is a git repo | |
if os.path.exists(os.path.join(absc, '.git')): | |
print("Detected as a git repository") | |
sync_type = 'git' | |
try: | |
#Only checking for a remote called origin. Anything more fancy will fail | |
args = ['-C', absc, 'config', '--get', 'remote.origin.url'] | |
sync_uri = subprocess.check_output(['git'] + args).decode("utf-8").strip() | |
#Special case for the base libraries as they were the | |
#only ones to be updated during a 'fusesoc update' | |
auto_sync = sync_uri in BASE_LIBS | |
except subprocess.CalledProcessError: | |
logger.warn("Failed to get remote URL. Ignoring") | |
continue | |
#Local directory | |
else: | |
print("Detected as local library") | |
auto_sync = True | |
sync_uri = None | |
sync_type = 'local' | |
secname = 'library.'+name | |
config.add_section(secname) | |
config.set(secname, 'location', location) | |
if not auto_sync: | |
config.set(secname, 'auto-sync', 'false') | |
if sync_type != 'local': | |
config.set(secname, 'sync-uri', sync_uri) | |
config.set(secname, 'sync-type', sync_type) | |
#TODO: Only remove entries that were successfully converted to libraries? | |
if cores_root: | |
config.remove_option('main', 'cores_root') | |
if systems_root: | |
config.remove_option('main', 'cores_root') | |
with open(f, 'w') as _f: | |
config.write(_f) | |
if __name__ == "__main__": | |
upgrade_config(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment