Skip to content

Commit

Permalink
git clone added to azure quickstart
Browse files Browse the repository at this point in the history
  • Loading branch information
lee212 committed Oct 10, 2016
1 parent 96a7f36 commit b27a4ef
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 28 deletions.
105 changes: 85 additions & 20 deletions simpleazure/azure_quickstart_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from github_cli import GithubCLI
import json
import inspect
import os.path
import collections

class AzureQuickStartTemplates(object):
"""Constructs a :class:`AzureQuickStartTemplates <AzureQuickStartTemplates>`.
Expand Down Expand Up @@ -45,32 +47,61 @@ def get_list(self):
return func()

def get_list_cli(self):
return self.cli.get_list()
res = collections.OrderedDict()
items = self.cli.get_list()
for item in items:
if item == ".github":
continue
if item == "1-CONTRIBUTION-GUIDE":
continue

try:
# requred
azuredeploy = self.get_azuredeploy_cli(item)
parameters = self.get_parameters_cli(item)
meta = self.get_metadata_cli(item)
except:
continue
etc = self.get_all_cli(item)
nested = self.get_nested_cli(item)
scripts = self.get_scripts_cli(item)
res[item] = {
"azuredeploy": azuredeploy,
"parameters": parameters,
"metadata": meta,
"nested": nested,
"scripts": scripts,
"etc": etc
}
meta = azuredeploy = parameters = nested = scripts = etc = ""
return res

def get_list_api(self):

self.api.set_var("repo", self.git_repo)
self.api.set_var("owner", self.git_owner)
#res = [ item if item['type'] == "dir" for item in self.api.get_list() ]
res = {}
res = collections.OrderedDict()

items = self.api.get_list()
for item in items:
if item['type'] != "dir":
continue
if item['name'] == ".github":
continue
if item['name'] == "1-CONTRIBUTION-GUIDE":
continue
try:
# required files
azuredeploy = self.get_azuredeploy(item['path'])
parameters = self.get_parameters(item['path'])
meta = self.get_metadata(item['path'])
azuredeploy = self.get_azuredeploy_api(item['path'])
parameters = self.get_parameters_api(item['path'])
meta = self.get_metadata_api(item['path'])
except:
# If fails, skip to next
continue
etc = self.get_all(item['path'])
nested = self.get_nested(item['path'])
scripts = self.get_scripts(item['path'])
etc = self.get_all_api(item['path'])
nested = self.get_nested_api(item['path'])
scripts = self.get_scripts_api(item['path'])
res[item['name']] = {
"azuredeploy": azuredeploy,
"parameters": parameters,
Expand All @@ -82,35 +113,69 @@ def get_list_api(self):
meta = azuredeploy = parameters = nested = scripts = etc = ""
return res

def get_metadata(self, path):
def get_metadata_api(self, path):
"""Returns cotents of the metadata.json file from a path"""
return self._get_json_contents(path + "/metadata.json")
self.api.set_var("path", path + "/metadata.json")
return self._get_json_contents(self.api.get_file())

def get_azuredeploy(self, path):
def get_azuredeploy_api(self, path):
"""Returns cotents of the azuredeploy.json file from a path"""
return self._get_json_contents(path + "/azuredeploy.json")
self.api.set_var("path", path + "/azuredeploy.json")
return self._get_json_contents(self.api.get_file())

def get_parameters(self, path):
def get_parameters_api(self, path):
"""Returns cotents of the azuredeploy.parameters.json file from a path"""
return self._get_json_contents(path + "/azuredeploy.parameters.json")
self.api.set_var("path", path + "/azuredeploy.parameters.json")
return self._get_json_contents(self.api.get_file())

def get_nested(self, path):
def get_nested_api(self, path):
"""Returns nested templates from a path"""
self.api.set_var("path", path + "/nested")
return self.api.get_list() or ""

def get_scripts(self, path):
def get_scripts_api(self, path):
"""Returns scripts from a path"""
self.api.set_var("path", path + "/scripts")
return self.api.get_list() or ""

def get_all(self, path):
def get_all_api(self, path):
"""Returns all items from a path"""
self.api.set_var("path", path)
return self.api.get_list()

def _get_json_contents(self, path):
self.api.set_var("path", path)
data = json.loads(self.api.get_file())
def _get_json_contents(self, content):
try:
data = json.loads(content)
except Exception as e:
#TODO
# Some json data could not be decoded
# e.g. /azure-quickstart-templates/201-logic-app-veter-pipeline/azuredeploy.json
#print ("Failed to read json, {0}".format (e))
data = content
return data

def get_metadata_cli(self, path):
content = self.cli.get_file(os.path.join(path, "metadata.json"))
print (path)
return self._get_json_contents(content)

def get_azuredeploy_cli(self, path):
content = self.cli.get_file(os.path.join(path, "azuredeploy.json"))
print (path)
return self._get_json_contents(content)

def get_parameters_cli(self, path):
content = self.cli.get_file(os.path.join(path, "azuredeploy.parameters.json"))
print (path)
return self._get_json_contents(content)

def get_nested_cli(self, path):
return self.cli.get_list(os.path.join(path, "nested")) or ""

def get_scripts_cli(self, path):
return self.cli.get_list(os.path.join(path, "scripts")) or ""

def get_all_cli(self, path):
return self.cli.get_list(path)


29 changes: 21 additions & 8 deletions simpleazure/github_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ def get_list(self, path=None):
return
if not path:
path = self.local_path
dirnames = [ d for d in os.listdir(path) if
not os.path.isfile(os.path.join(path, d))]
try:
dirnames = [ d for d in os.listdir(path) if
not os.path.isfile(os.path.join(path, d))]
except Exception as e:
dirnames = []
# TODO: Delete (azure quickstart templates only)
if '.github' in dirnames:
del(dirnames[dirnames.index('.github')])
Expand All @@ -35,16 +38,26 @@ def get_file(self, path):
if not self.cloned:
print ("fatal: clone first")
return
with open(path, "r") as f:
content = f.read()
try:
with open(os.path.join(self.local_path, path)) as f:
content = f.read()
except IOError as e:
with open(path, "r") as f:
content = f.read()
except Exception as e:
print ("fatal: {0}".format(e))
return

return content

def clone(self, path=None):
path = path or self.path
def set_repo(self, path):
self.path = path
basename = os.path.basename(path).split(".")[0]
self.local_path = os.path.join(config.DEFAULT_PATH, basename)
self.name = os.path.basename(path).split(".")[0]
self.local_path = os.path.join(config.DEFAULT_PATH, self.name)

def clone(self, path=None):
path = path or self.path
self.set_repo(path)
# IF exists, git pull
if self.is_cloned():
sh.cd(self.local_path)
Expand Down

0 comments on commit b27a4ef

Please sign in to comment.