Thursday, June 16, 2011

Best way to embed source code in Blog


I have came across to many ways in embedding source code in Blogger but found the best one as Vim. The best way is to write open your source code in Gvim and from the "Syntax" choose "Convert to HTML". gVim creates the file .html file with the same name, for example like if you are using "abc.py" it will create "abc.py.html". Following are the steps to use its code:

  1. The file generated would have code like given below you need to copy the text between the style tags an remove the body tag as it may distort your blog's main formatting. (although I have used "Consolas" it is by default "monospace") :
  2. <style type="text/css">
    <!--
    pre { font-family: consolas; color: #000000; background-color: #ffffff; }
    .Constant { color: #ff00ff; }
    .Identifier { color: #008080; }
    .Statement { color: #804040; font-weight: bold; }
    -->
    </style> 
     
  3. Use <pre> </pre> tags to write the code in HTML editing mode for the blog just copy the converted code in between of it and the good thing is that gVim produces HTML escaped characters. Example of a python program has been given below for binary search.
  4. def search(sequence, number, lower=0, upper=None):
            if upper is None: upper = len(sequence)-1
            if lower == upper:
                    assert number == sequence[upper]
                    return upper+1
            else:
                    middle = (lower + upper) // 2
                    if number > sequence[middle]:
                            return search(sequence, number, middle+1, upper)
                    else:
                            return search(sequence, number, lower, middle) 
Other ways includes the use of Javascript Library (Syntax Highlighter Code Project) its client side scripting and support languages like C++, C#, CSS, Delphi, Pascal, Java, Javascript, PHP, Python, Ruby, SQL, Visual Basic, HTML, XML etc. Major issues in using this are adding .js and .css files and also making changes in master template and right angle characters must be HTML escaped, if you are a blogging novice, I advice not to use it.

Some other uses methods include external code hosting projects like Pastie, Snipplr etc in which my personal favorite is pastie.org but it also have some issues of size and code syntax. see the code snippet below.



As you can see the code in the 2nd line if haven't taken to the 3rd one it by passes the boundary and goes out of frame, so its also not well suited for this purpose. It comes with an advantage that it allows conversion into plain text and direct download of the code.

Monday, May 23, 2011

Git - Version Control System

An Introduction to GIT - 
Git is open source distributed version control system developed by Linus Torvalds to handle projects with great speed and efficiency ... Learn more

Scenario (what I have been working in) - 
I have a server of Ubuntu on Amazon EC2 and there I hosted one Django project on which I used to commit and deploy the code frequently.


Checking Out ... 

you need to generate an SSH key and set it up.
ssh-keygen
(You can leave everything to default when keygen asks you questions, thats the easiest way to go along)
Run this command next, and copy the output to clipboard or something..
cat ~/.ssh/id_rsa.pub

Now login to development server:
ssh -i amazonkey.pem ubuntu@myproject.bharat.in 

Then update the key as:
cd .ssh
cat >> authorized_keys
(mind the double redirection operator to append to the file)
paste the copied string, and you are good to go.

Next, clone from GIT.
git clone ubuntu@devel.bharat.in:/opt/repos/myproject.git

(Afterwards, you'll pull to get the latest changes as)
git pull origin master

You'll get a directory called myproject in your working directory, cd into it and fire..

python manage.py runserver

(That should be all to run the development instance)

Pushing/Commiting/Deploying etc etc

When you are done working, you may want to commit the changes..

git commit -a -m "Some message"

IMP TIP: If the message contains refs #ISSUEID, the commit will automatically be associated with the issue in its log,
if the message contains fixes #ISSUEID, the issue will be marked resolved,
and if the message contains closes #ISSUEID, the issue will be closed. *

Then you may push the changes to the server back as:
git push origin master

(All the commits from the last push in the branch master)

Finally to deploy, add a remote (one time)
git remote add stage ubuntu@myproject.bharat.in:/opt/repos/myprojectstage.git

All you have to do is to push into this remote's master as:
git push stage master

(The changes will immediately reflect at http://myproject.bharat.in)

Production remote:

git remote add production ubuntu@bharat.in:/home/ubuntu/repos/myprojectprod

Admin remote:

git remote add admin ubuntu@stage.bharat.in:/opt/repos/admin.git

Note for Beginners - 
Don't loose your heart if you don't have a Amazon EC2 or stuff like that ... you can start doing it using GitHub Please have a note that you have Git installed on any of the operating system you use. You can have git download "Jargon" at Git - Fast Version Control System.

# for this you need to first have a account on GitHub
# open git bash through start up launcher and check for .ssh if not then do 

ssh-keygen -t rsa -C "youremail@provider.domain"

# now come to .ssh and copy public key's content to github site

cat id_rsa.pub

# After doing it fine to check use this command 

ssh -T git@github.com

# If it works well then do it like 

git config --global user.name "FirstName LastName"
git config --global user.email "handle@provider.domain"

git config --global github.user githubusername (bindian0509)
git config --global github.token the_token_you_have_seen_on_github_website

# now go to the project directory which you have specified in the website (like mine "bharat")

cd bharat

git init

# to add a sample file

touch README.txt

git add -A (to add all the files present in your directory)
git add README (to add only readme)

git commit -a -m "Message"
git commit -m "Message"

# for adding a remote origin to github repos

git remote add origin git@github.com:bindian0509/bharat.git

# now push the code to 

git push origin master

# and now you are done with it

Wednesday, June 25, 2008

Is Open Source Code Secure?

The contention that open-source code programs are more reliable and secure will hold no water. Those who argue in favor of this assertion believe that an open-source code system is tested by a huge number of people on the code level who discover all possible errors. Yes, testing for errors on the code level concurrently with is easy and effective, but the results of such testing are far from ideal. Even after extensive testing by thousands of users, errors crop up in Linux. Moreover, judging from the army of users that tested the latest Windows versions, you would think that it would finally become the perfect operating system. We do know better than that, don't we? Testing is one thing, but running under real-life conditions is another, with unpredictable results popping up.


Linux support is rather expensive, so you might encounter problems obtaining timely updates. Moreover, administering Linux is more difficult than administering Windows. But why is Linux so difficult to master? The answer is simple: Performance and convenience are two incompatible things. Linux is a performance product, and Windows is a convenience product.


Security is often sacrificed for performance or convenience. For example, some programs may have options that make the administrator's work easier (e.g., the PHP interpreter may have the debug option enabled) but at the same time make it easier for hackers to break into the system. Nevertheless, Linux security is higher than that of Windows, and this has nothing to do with it being open source. Simply, many security-related aspects in Linux are implemented better than in Windows. For Example you can see the today's pathetic situation of a windows effected by viruses or malicious programs. Other examples includes the memory allocation of programs in Linux, like in normal circumstances a program cannot oversteps the bound of allocated memory whereas in Windows any program can use the any memory area resulting in the danger of faulty operations or system crash if the memory area of one program being over written by other.


So over all you can grade LINUX far much better for Quality and Development than windows and once you get into it ... you will never look back and if you are still in catch22 try to re-recognize your needs and then decide!!