check_versions.py 6.07 KB
Newer Older
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
#!/usr/bin/env python

# Check software versions on the target

__author__ = "Elphel"
__copyright__ = "Copyright 2016, Elphel, Inc."
__license__ = "GPL"
__version__ = "3.0+"
__maintainer__ = "Oleg K Dzhimiev"
__email__ = "oleg@elphel.com"
__status__ = "Development"

import json
import os
import subprocess
import sys
    
#http://stackoverflow.com/questions/287871/print-in-terminal-with-colors-using-python
class bcolors:
  HEADER = '\033[95m'
  OKBLUE = '\033[94m'
  OKGREEN = '\033[92m'
  WARNING = '\033[38;5;214m'
  FAIL = '\033[91m'
  ENDC = '\033[0m'
  BOLD = '\033[1m'
  BOLDWHITE = '\033[1;37m'
  UNDERLINE = '\033[4m'

def shout(cmd):
  subprocess.call(cmd,shell=True)

def command_over_ssh(addr,command):
    cmd = "ssh "+addr+" "+command
    print("cmd: "+cmd)
    try:
      ret = subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True)
    except subprocess.CalledProcessError:
      raise Exception("ssh to target requires access by public key. Run: \033[1;37mssh-copy-id "+addr+"\033[0m")
    return ret.strip() 

def read_local_conf(conf_file,pattern):
  ret = "0"
  if os.path.isfile(conf_file):
    with open(conf_file,"r") as f:
      lines = f.readlines()
      for line in lines:
        test = line.find(pattern)
        if test!=-1:
          ret = line.split("=")[1].strip().strip("\"")
  return ret

def get_versions_from_target(addr,tdir):
  # print remote package list
  tmp_str = command_over_ssh(addr,"'ls "+tdir+"'")
  
  remote_list = []
  
  tmp_list = tmp_str.split()
  for elem in tmp_list:
    remote_list.append([elem,command_over_ssh(addr,"'cat "+tdir+"/"+elem+"'")])
  
  return remote_list

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
def get_versions_from_target_quick(addr,tdir):
  # print remote package list
  ldir = os.path.basename(tdir)
  if os.path.isdir(ldir):
    shout("rm -rf "+ldir)
    
  shout("scp -r "+addr+":"+tdir+" .")
  
  remote_list = []
  
  for f in os.listdir(ldir):
    with open(ldir+"/"+f, 'r') as content_file:
      content = content_file.read()
    remote_list.append([f,content.strip()])
  
  shout("rm -rf "+ldir)
  
  return remote_list

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
84
def get_version_from_git(path,vfile):
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
85
  #print(path)
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
  cwd = os.getcwd()
  os.chdir(cwd+"/"+path)
  
  p0=""
  p1=""
  
  if os.path.isfile(vfile):
    #PE.PV
    
    f=open(vfile)
    for line in f:
      line = line.strip()
      if (line[0]!="#"):
            break  
    p0 = line
    #PR
    
    cmd = "git rev-list --count $(git log -1 --pretty=format:\"%H\" "+vfile+")..HEAD"
    p1 = subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True)
    p1 = p1.strip()
        
  else:
    print(vfile+" file is missing in the project")
    
  os.chdir(cwd)
  return p0+"."+p1

def deep_analysis(local,remote):
  print("\nVersion analysis")
  
  print("\n"+bcolors.BOLDWHITE+"{:<24}".format("Project")+"{:<16}".format("Local GIT")+"{:<16}".format("Target Version")+bcolors.ENDC)
  
118
  update_list = ""
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
119 120 121 122 123 124 125 126 127 128 129
  for pl,vl in local:
    recstr = "{:<24}".format(pl)+"{:<16}".format(vl)
    prfound = False
    for pr,vr in remote:
      if pl==pr:
        prfound = True
        recstr = recstr+"{:<16}".format(vr)
        if vl==vr:
          recstr = bcolors.OKGREEN+recstr+bcolors.ENDC
        else:
          recstr = bcolors.FAIL+recstr+bcolors.ENDC
130
          pl = getname(pl,"","recipe_to_package")
131
          update_list = update_list+" bitbake "+pl+" -c target_scp -f\n"
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
132 133
    if not prfound:
      recstr = bcolors.WARNING+recstr+bcolors.ENDC
134
      pl = getname(pl,"","recipe_to_package")
135
      update_list = update_list+" bitbake "+pl+" -c target_scp -f\n"
136

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
137
    print(recstr)
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
138 139
  if not update_list=="":
    print("\nTo sync the software on the target run:\n"+update_list)
140 141 142 143 144 145 146 147 148

# all exceptions in one place
def getname(name,project,mode):
  global project_prefix
  global package_prefix
    
  if mode=="project_to_recipe":
    if name.find(project_prefix)==0:
      name = name[len(project_prefix):]
149 150 151

    if project.find(package_prefix)==0:
      name = package_prefix+name
152
      #only exception
153 154
    if name=="fpga-x393_sata":
      name="fpga-x393sata"
155 156 157 158 159 160 161 162 163 164
    return name
  
  elif mode=="recipe_to_package":
    if name=="linux-elphel":
      name = "linux-xlnx"
    elif name=="apps-php-extension":
      name = "php"
    return name
  else:
    return name
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

usage = """Usage example:
    {0}{1} root@192.168.0.9{2}, where
        192.168.0.9 - target ip address
        root        - target user
""".format(bcolors.BOLDWHITE,sys.argv[0],bcolors.ENDC)

# hardcoded
user = ""
ip = ""
local_conf = "poky/build/conf/local.conf"
target_dir = "/etc/elphel393/packages"
local_project_list = "projects.json"
local_dirs = ["rootfs-elphel","fpga-elphel","linux-elphel"]
git_vfile = "VERSION"
project_prefix = "elphel-"
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
181
package_prefix = "fpga-"
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
182 183 184 185 186 187 188 189 190 191 192

if len(sys.argv)>1:
  rootip = sys.argv[1].split("@")
  if len(rootip)>1:
    user,ip = rootip
  else:
    ip = rootip[0]
else:
  if (os.path.isfile(local_conf)):
    user   = read_local_conf(local_conf,"REMOTE_USER")
    ip = read_local_conf(local_conf,"REMOTE_IP")
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
193 194
    print(bcolors.WARNING+"NOTE: The default user and ip are taken from "+local_conf+bcolors.ENDC)
    print(bcolors.WARNING+"NOTE: To check against the latest code run ./setup.py first"+bcolors.ENDC)
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
195 196 197 198 199 200 201 202
    print(usage)
  
  if user=="" or ip=="":
    raise Exception(usage)

print("Software/firmware versions check for target "+bcolors.BOLDWHITE+user+"@"+ip+bcolors.ENDC)

print(bcolors.BOLDWHITE+"=== Read versions from the target ==="+bcolors.ENDC)
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
203
target_list = get_versions_from_target_quick(user+"@"+ip,target_dir)
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
204

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
205
#print(target_list)
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
206 207 208 209 210 211 212 213 214 215 216 217

print(bcolors.BOLDWHITE+"=== Read local versions ==="+bcolors.ENDC)
with open(local_project_list) as data_file:
  Projects = json.load(data_file)

local_list = []

for p,v in Projects.items():
  if p in local_dirs:
    if isinstance(v,dict):
      for k,l in v.items():
        tmp = get_version_from_git(p+"/"+k,git_vfile)
218
        name = getname(k,p,"project_to_recipe")
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
219
        local_list.append([name.encode('ascii','ignore'),tmp])
220
        
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
221 222 223 224 225 226
    elif isinstance(v,list):
      tmp = get_version_from_git(p,git_vfile)
      local_list.append([p.encode('ascii','ignore'),tmp])
    else:
      raise Exception("Unknown error")

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
227
#print(local_list)
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
228 229 230 231

deep_analysis(local_list,target_list)